package com.br.egaragens.adaptadores;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.crypto.spec.PSource;


import com.br.egaragens.R;
import com.br.egaragens.R.id;
import com.br.egaragens.R.layout;
import com.br.egaragens.entidades.Veiculo;
import com.br.egaragens.interfaces.IEntidade;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class VeiculoAdapter extends BaseAdapter{

	private SimpleDateFormat format = new SimpleDateFormat("dd/MM/yy");
	private SimpleDateFormat dtForm = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
	private static DecimalFormat formatoMoeda = new DecimalFormat("##,###,###,##0.00");
	private Context mContext;
	private List<Veiculo> lVeiculos;
	private Veiculo veiculo;
	private LayoutInflater inflater;
	private ListView lView;
	
	public VeiculoAdapter(Context context, ListView listView, List<Veiculo> list){
		this.mContext = context;
		this.lVeiculos = list;
		this.lView = listView;
		inflater = LayoutInflater.from(context);
		
	}
	
	public int getCount() {
		return lVeiculos.size();
	}

	public IEntidade getItem(int position) {
		return lVeiculos.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View v;

		if (convertView == null) {
			v = inflater.inflate(layout.listview_veiculos, null);

		} else {
			v = convertView;
		}
		EditText imageView = (EditText) v.findViewById(id.edtListPlaca);
		TextView txtDataHoraEntrada = (TextView) v.findViewById(id.txtListDtHoraEntrada);
		TextView txtTipoVeiculo = (TextView) v.findViewById(id.txtListTipoVeiculo);
		TextView txtHorasDecorrente = (TextView) v.findViewById(id.txtListHorasDecorrente);
		
		veiculo = (Veiculo) lVeiculos.get(position);
		Date d;
		try {
			d = dtForm.parse(veiculo.getDtHoraEntrada().toString());
		
		Date dSaida= new Date(System.currentTimeMillis());
		long difHoras = (dSaida.getTime() - d.getTime());
		int hora =(int)difHoras /1000/60/60;
		int minutos= (int) difHoras /1000 /60 %60;
		int t = dSaida.getMinutes()-d.getMinutes();
		t= t < 0 ? t+60 : t ;
		imageView.setText(veiculo.getPlaca());
		txtDataHoraEntrada.setText("Ent: "+format.format(d)+" - "+d.getHours()+"h"+(d.getMinutes() < 10?"0"+d.getMinutes():d.getMinutes()));
		txtTipoVeiculo.setText("Veículo "+veiculo.getTipo());
		txtHorasDecorrente.setText("Tempo decorrido: "+hora+"h"+(t < 10 ? "0"+t : t));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return v;
	}

}
